package ktvdownloadserver

import (
	"fmt"
	"ktvserver/core/tcpserver"
)

// 处理函数路由

type Route struct {
	Cmd     uint16
	Handler tcpserver.Handler
}

var routes = []Route{
	// 系统文件处理函数
	{REQUEST_SYSTEM_FILEINFO, tcpserver.HandlerFunc(KtvSystemFileInfo)},
	{REQUEST_SYSTEM_FILEDATA, tcpserver.HandlerFunc(KtvSystemFileData)},
	{REQUEST_SYSTEM_CONTINUE, tcpserver.HandlerFunc(KtvSystemFileContinue)},

	// 歌曲文件处理函数
	{REQUEST_SONG_FILEINFO, tcpserver.HandlerFunc(KtvSongFileInfo)},
	{REQUEST_SONG_FILEDATA, tcpserver.HandlerFunc(KtvSongFileData)},
	{REQUEST_SONG_CONTINUE, tcpserver.HandlerFunc(KtvSongFileContinue)},
	{REQUEST_SONG_IP_LIST, tcpserver.HandlerFunc(KtvSongIpList)},

	// 电影文件处理函数
	{REQUEST_MOVIE_FILEINFO, tcpserver.HandlerFunc(KtvMovieFileInfo)},
	{REQUEST_MOVIE_FILEDATA, tcpserver.HandlerFunc(KtvMovieFileData)},
	{REQUEST_MOVIE_CONTINUE, tcpserver.HandlerFunc(KtvMovieFileContinue)},

	// 下载服务器处理函数
	{REQUEST_PUBLIC_SONG_FILELIST, tcpserver.HandlerFunc(KtvPublicSongFileList)},
	{REQUEST_PUBLIC_SONG_DATA, tcpserver.HandlerFunc(KtvPublicSongFileData)},
	{REQUEST_PUBLIC_SONG_IP, tcpserver.HandlerFunc(KtvPublicSongIP)},

	//  旧的歌曲同步协议，直接返回空内容
	{REQUEST_PUBLIC_SONGDATA_BEYOND_NUMBER, tcpserver.HandlerFunc(KtvPublicBeyondNumberSongDataEmpty)},

	// 新的同步歌曲协议
	{REQUEST_PUBLIC_NEW_SONGDATA_BEYOND_NUMBER, tcpserver.HandlerFunc(KtvPublicBeyondNumberSongData)},

	//
	{REQUEST_DOWNLOAD_SONG_IP_LIST, tcpserver.HandlerFunc(KtvDownloadSongIpList)},
}

// 处理函数示例
func handleNotFound(w tcpserver.ResponseWriter, req *tcpserver.Request) {
	fmt.Printf("TcpServer handleNotFound, Cmd=0x%x, Len=%d\n", req.Cmd, len(req.DataBody))

	// 写入空数据
	tcpserver.WriteHeader(w, 0, 0)
	tcpserver.WriteFooter(w)
}

// 获取服务器的命令代码处理函数映射
func GetServerMux() *tcpserver.ServeMux {

	myMux := tcpserver.NewServeMux()

	for i := 0; i < len(routes); i++ {
		myMux.Handle(routes[i].Cmd, routes[i].Handler)
	}

	// 注册notfound
	myMux.HandleNotFound(tcpserver.HandlerFunc(handleNotFound))

	return myMux
}
